package com.github.fujianlian.klinechart;

import com.github.fujianlian.klinechart.entity.h.ICandle;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据适配器
 * Created by tifezh on 2016/6/18.
 */
public class KLineChartAdapter extends BaseKLineChartAdapter {

    private List<KLineEntity> datas = new ArrayList<>();

    public KLineChartAdapter() {

    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public ICandle getItem(int position) {
        return datas.get(position);
    }

    @Override
    public int indexOfDate(String data) {

        if (datas.size() > 0) {
            for (KLineEntity kLineEntity : datas) {
                if (kLineEntity.getDate().equals(data)) {
                    return datas.indexOf(kLineEntity);
                }
            }
        }

        return -1;
    }

    @Override
    public String getDate(int position) {
        return datas.get(position).Date;
    }

    /**
     * 向头部添加数据
     */
    public void addHeaderData(List<KLineEntity> data) {
        if (data != null && !data.isEmpty()) {
            datas.clear();
            datas.addAll(data);
        }
    }

    /**
     * 向尾部添加数据
     */
    public void addFooterData(List<KLineEntity> data) {
        if (data != null && !data.isEmpty()) {
            datas.addAll(0, data);
            sortDataTimes(datas);
        }
    }

    public void setNewDatas(List<KLineEntity> data) {
        if (data != null && !data.isEmpty()) {
            datas.clear();
            datas.addAll(0, data);
            sortDataTimes(datas);
        }
    }

    private void sortDataTimes(List<KLineEntity> datas) {
        for (KLineEntity data : datas) {
            String date = data.getDate();
            if (date.length() > 10) {
                data.setDate(date.substring(5, date.length() - 3));
            }
        }


    }

    /**
     * 改变某个点的值
     *
     * @param position 索引值
     */
    public void changeItem(int position, KLineEntity data) {
        datas.set(position, data);
        notifyDataSetChanged();
    }

    /**
     * 数据清除
     */
    public void clearData() {
        datas.clear();
        notifyDataSetChanged();
    }
}
